var m3958_host = 'http://www.m3958.com/';

(function(){
	var hr = window.location.href.toUpperCase();
	if(hr.indexOf('8888') == -1){
		if(hr.indexOf('debug=true') != -1){
			m3958_host = 'file:///E:/eclipse-jee-helios-SR1-win32/xyzcms/war/';
		}
	}else{
		m3958_host = 'http://127.0.0.1:8888/';
	}
})();

var fireImgFetchGlobalEvent = function (e, param) {//'imgPreFetched'
	  YUI().use('event-custom', function (Y) {
	    var publisher = new Y.EventTarget();
	    publisher.publish(e, {
	      broadcast:  2,   // global notification
	      emitFacade: true // emit a facade so we get the event target
	    }).fire(param);
	  });
};

if(!m3958_site_config){
	alert('请在页面Header上部配置m3958_site_config对象');
}

var m3958_groups = {},
	m3958_use_array = ['javascript-site','javascript-section','javascript-article','section-widget'];

/*站点基本数据模型*/
(function(){
	m3958_groups["m3958-javascript-site"] = {
			combine : true,
			base : m3958_host,
			charset: 'utf-8',
			root : '',
			comboBase: m3958_host + 'combo/?',
			modules:{
				'javascript-site' : {
			        path: 'm3958-module-js/javascriptsite.js&1234'
				},
				'javascript-section' : {
					path : 'm3958-module-js/javascriptsection.js'
				},
				'javascript-article' : {
//					requires:[],
					path : 'm3958-module-js/javascriptarticle.js'
				},
				'section-widget' : {
					path : 'm3958-module-js/section-widget.js'
				}
			}
	};
})();

/*首页幻灯*/
if(m3958_site_config.frontPageSlideShow){
	var modules = {},
		ssConfig=m3958_site_config.frontPageSlideShow,
		csspath = ssConfig.slideShowCssPath;
	if(!csspath){
		csspath = m3958_host + 'm3958-module-css/frontpage-slideshow.css';
	}
	modules["frontpage-slideshow-css"] = {type: "css",
										fullpath : csspath
    									};
	modules["frontpage-slideshow"] = {
            fullpath:  m3958_host + 'm3958-module-js/frontpage-slideshow.js'
        };
	m3958_use_array.push('frontpage-slideshow-css');
	m3958_use_array.push('frontpage-slideshow');

	m3958_groups["m3958-frontpage-slideshow"] = {
			combine : false,
	        modules: modules
	    };
}

/*站点菜单*/
if(m3958_site_config.mainMenu){
	var mmconfig = m3958_site_config.mainMenu,
		modules = {};
	if(mmconfig.mainMenuCssPath){
		modules["node-menunav-core-css"] = {type: "css",
											path: "node-menunav/assets/node-menunav-core.css"
        									};
		modules["custommenunavskin"] = {
                type: "css",
                fullpath:  m3958_host + mmconfig.mainMenuCssPath,
                supersedes: ['skin-sam-node-menunav']//替換對象
            };
		m3958_use_array.push('node-menunav-core-css');
		m3958_use_array.push('custommenunavskin');
	}
	
	modules["menu-html-creator"] = {
			fullpath : m3958_host + 'm3958-module-js/menu-html-creator.js'
	};
	
	m3958_use_array.push('node-menunav');
	m3958_use_array.push('menu-html-creator');
	m3958_groups["m3958-main-menu"] = {
			combine : false,
	        modules: modules
	    };
}
	
YUI({combine: true,
	"maxURLLength":1024,
	groups:m3958_groups
}).use(m3958_use_array,function(Y){
	/*
	 * m3958_site_config = {siteId:28783,mainMenuContainer:'#abc',mainMenuCssPath:'/'};
	 * 
	 */
//	Y.log(m3958_groups);
//	Y.log(m3958_use_array);
	var jsite = new Y.M3958.JavascriptSite({siteId : m3958_site_config.siteId}),
		mainMenuContainerNode;
	
	jsite.on('siteReady',function(e){
	    if(m3958_site_config.mainMenu){
	    	mainMenuContainerNode = Y.one(m3958_site_config.mainMenu.mainMenuContainer);
	    	if(!mainMenuContainerNode){
	    		alert('您指定了主菜单的ID是：' + m3958_site_config.mainMenuContainer + ',但是页面上没有这个Id的DIV！');
	    		return;
	    	}
			var mc = new Y.M3958.MenuHtmlCreator({menuItems: jsite.site.linkList});
//			Y.log(mc);
//			Y.log(jsite.site.linkList);
			var mh = mc.getMenuHtml(); 
//			Y.log(mh);
			mainMenuContainerNode.setContent(mh);
			var ch = mainMenuContainerNode.one('#m3958-menu--1');
			if(ch)ch.plug(Y.Plugin.NodeMenuNav/*,{mouseOutHideDelay:750000}*/);
	    }
	    
	    if(m3958_site_config.frontPageSlideShow){
	    	var ssConfig = m3958_site_config.frontPageSlideShow;
	    	if(!ssConfig.slideShowTags){
	    		alert('请指定幻灯播放的容器！');
	    		return;
	    	}
			var fp = new Y.M3958.FrontpageSlideShow({srcNode : ssConfig.frontPageSlideContainer,
													'siteId':jsite.site.id,
													'tags':ssConfig.slideShowTags,
													render:true,
													autoSlide:true,
													interval:3,
													height: 268,
													imgSize:'320x240',
													numBoxAlign:'horizontal'});
			fp.fetchArticles(0,5);
	    }
	    
	    var sectioncs = Y.all('.m3958-section-container');
	    sectioncs.each(function(item){
	    	var splitIds = item.get('id').split('-'),
	    		sid = splitIds.slice(-1)[0];
	    	var section = jsite.findSection(parseInt(sid)),
	    		sectionWidget = new Y.M3958.SectionWidget({section:section,sectionConfig:getSectionConfig(sid),srcNode:item,render:true});
	    	sectionWidget.displaySection(0,getSectionConfig(sid, 'articleNum'));
	    });
	    
	});
	jsite.fetchSite();
	
	function getSectionConfig(sid,field){
		var sg = Y.Array.find(m3958_site_config.sectionConfigs,function(it){
			if(it[sid]){
				return true;
			}
		});
		if(!field){
			return sg || m3958_site_config.sectionConfigBase;
		}
		return (sg && sg[field]) || m3958_site_config.sectionConfigBase[field];
	}
});